//引入vue模块
import vue from "vue";
//下载路由,引进来
import vueRouter from "vue-router";
//使用路由
vue.use(vueRouter);

// import index from "../components/index.vue";
import home from "../components/home.vue";
import knowledgeFile from "../components/knowledgeFile/knowledgeFile.vue";
import workProgress from "../components/workProgress/workProgress.vue";
// import deviceManage from "../components/deviceManage.vue";
import addInfo from "../components/addInfo/addInfo.vue";
import message from "../components/message.vue";
import modifyPassword from "../components/modifyPassword.vue";
import login from "../components/login/login.vue";
import deviceDetail from "../components/deviceDetail/deviceDetail.vue";
import formList from "../components/formList.vue";
import addReport from "../components/addReport/addReport.vue";


const route =[
    {
        path:"/",
        component:home,
        name:"home",
        redirect:"/index",
        children:[
            {path:"index",name:'index',component: () => import("@/components/index/index"),meta:{ requireAuth: true}},
            {path:"workProgress",component:workProgress,meta:{ requireAuth: true}},//meta:{ requireAuth: true}路由拦截，判断该路由的访问是否需要登录
            {path:"knowledgeFile",component:knowledgeFile,meta:{ requireAuth: true}},
            {path:"deviceManage",name:"deviceManage",component: () => import("@/components/deviceManage/deviceManage")}
        ]
    },
    {
        path:"/addInfo",
        component:addInfo,meta:{ requireAuth: true},
        name:"addInfo",
    },
    {
        path:"/message",
        component:message,
        name:"message"
    },
    {
        path:"/modifyPassword",
        component:modifyPassword,
        name:"modifyPassword"
    },
    {
        path:'/deviceDetail',
        component:deviceDetail,meta:{ requireAuth: true},
        name:'deviceDetail'
    },
    {
        path:"/login",
        component:login,
        name:"login",
    },
    {
        path:"/formList",
        component:formList,meta:{ requireAuth: true},
        name:"formList",
    },
    {
        path:"/addReport",
        component:addReport,meta:{ requireAuth: true},
        name:"addReport",
    }    
]
//创建路由实例
const router = new vueRouter({
    routes:route
})

router.beforeEach((to, from, next) => {
    // window.console.log(from)
    // window.console.log(to)
//r => (r.meta.requireAuth)
    if(to.matched.some(function(r){
        return r.meta.requireAuth
    })){
        if(localStorage.getItem('_islogin')=='1'){
            next()
        } else {
            // 没有登陆的时候跳转到登陆界面
            next({
                name: 'login'
            });
        }
            return
        }
        next()
  })

//输出router实例
export default router;